package com.jyy.prefabricated.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jyy.prefabricated.common.AjaxResult;
import com.jyy.prefabricated.dto.Pageable;
import com.jyy.prefabricated.entity.SysUser;
import com.jyy.prefabricated.entity.TrainingOrganizationRegister;
import com.jyy.prefabricated.service.SmsRecordService;
import com.jyy.prefabricated.service.TrainingOrganizationRegisterService;
import com.jyy.prefabricated.utils.UserUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 机构注册
 * @version 1.0.0
 * @author: zjj
 */
@Slf4j
@RequestMapping("/api")
@RestController
public class TrainingOrganizationRegisterController {

    @Autowired
    private UserUtils userUtils;

    @Autowired
    private TrainingOrganizationRegisterService trainingOrganizationRegisterService;

    @Autowired
    private SmsRecordService smsRecordService;

    /**
     * 新增注册
     * @param trainingOrganizationRegister
     * @return
     */
    @PostMapping("/training-organization-register")
    public AjaxResult insert(@RequestBody TrainingOrganizationRegister trainingOrganizationRegister){
        log.info("Rest request to register training-organization:{}",trainingOrganizationRegister);
        smsRecordService.checkCode(trainingOrganizationRegister.getTel(),trainingOrganizationRegister.getValidateCode());
        int id = trainingOrganizationRegisterService.insert(trainingOrganizationRegister);
       // TrainingOrganizationRegister result = trainingOrganizationRegisterService.selectById(id);
        return AjaxResult.success();
    }

    /**
     * 查询注册详情
     * @param id
     * @return
     */
    @GetMapping("/training-organization-register/{id}")
    public AjaxResult selectById(@PathVariable Integer id){
        log.info("Rest request to apply training-organization-register:{}",id);
        TrainingOrganizationRegister result = trainingOrganizationRegisterService.selectById(id);
        return AjaxResult.success(result);
    }

    /**
     * 通过审核
     * @param id
     * @return
     */
    @PutMapping("/training-organization-register-apply/{id}")
    public AjaxResult apply(@PathVariable Integer id){
        log.info("Rest request to apply training-organization-register:{}",id);
        SysUser user = userUtils.getCurrentUser();
        trainingOrganizationRegisterService.apply(id, user);
        return AjaxResult.success();
    }

    /**
     * 驳回审核
     * @param id
     * @return
     */
    @PutMapping("/training-organization-register-reject/{id}")
    public AjaxResult reject(@PathVariable Integer id){
        log.info("Rest request to reject training-organization-register:{}",id);
        SysUser user = userUtils.getCurrentUser();
        trainingOrganizationRegisterService.reject(id, user);
        return AjaxResult.success();
    }

    /**
     * 分页查询机构注册列表
     * @param keyword 关键字
     * @param startDate 开始日期
     * @param endDate 结束日期
     * @param applyStatus 审核状态
     * @param pageable 分页信息
     * @return
     */
    @GetMapping("/training-organization-register")
    public AjaxResult selectByParams(@RequestParam(required = false, defaultValue = "") String keyword,@RequestParam(required = false, defaultValue = "") String startDate,@RequestParam(required = false, defaultValue = "") String endDate, @RequestParam(required = false) Integer applyStatus, Pageable pageable){
        log.info("Rest request to select training-organization-register by keyword:{},startDate:{},endDate:{},applyStatus:{}",keyword,startDate,endDate,applyStatus);
        PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<TrainingOrganizationRegister> trainingOrganizationRegister = trainingOrganizationRegisterService.selectByParams(keyword, startDate, endDate, applyStatus);
        PageInfo<TrainingOrganizationRegister> result = new PageInfo<>(trainingOrganizationRegister);
        return AjaxResult.success(result);
    }
}
